import { createApp } from "vue";
import App from "./App.vue";
import router from "./router";
import { createPinia } from "pinia";
import * as ElementPlusIconsVue from "@element-plus/icons-vue";
import "@/assets/less/index.less";
import "@/assets/less/reset.less";
import "@/api/mock";
import api from "@/api/api";
import { useAllDataStore } from '@/stores'
// import ElementPlus from "element-plus";
// import "element-plus/dist/index.css";
function isRoute(to:any){
  return router.getRoutes().filter(item=>item.path===to.path).length>0
}
router.beforeEach( (to, from) => {
  //如果要跳转的不是login,且token不存在(可以通过不存在token判断出用户未登录)
  if(to.path !== "/login" && !store.state.token){
    //跳转到login
    return { name: "login" };
  }
  //如果路由记录不存在
  if(!isRoute(to)){
    //跳转到404
    return { name: "404" };
  }
});
const app = createApp(App);
const pinia = createPinia();
app.use(pinia);
const store = useAllDataStore();
app.config.globalProperties.$api = api;
store.addMenu(router,"refresh")
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component);
}
app.use(router).mount("#app");

